package com.jackrain.nea.fc.cp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.fc.cp.api.CadUserPwdmodifyCmd;
import com.jackrain.nea.fc.cp.api.CadUserSaveCmd;
import com.jackrain.nea.fc.cp.api.CadorgSaveCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * Created by ZCY on 2017/12/14.
 */
@Api(value = "CP_C_HRUSERS CP_CHRORG表", description = "AD域组织或用户新增|修改密码")
@RestController
public class CadorgCtrl {

    @Reference(version = "1.0", group = "cp")
    private CadorgSaveCmd cadorgSaveCmd;
    @Reference(version = "1.0", group = "cp")
    private CadUserSaveCmd cadUserSaveCmd;
    @Reference(version = "1.0", group = "cp")
    private CadUserPwdmodifyCmd cadUserPwdmodifyCmd;
    @ApiOperation(value = "组织的新增")
    @RequestMapping(path = "/api/cadorgcmdimpl/save", method = RequestMethod.POST)
    public JSONObject cpchruserssaves(HttpServletRequest request,
                                      @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", JSON.parseObject(param));
//            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.cp.api.CadorgSaveCmd", "cp", "1.0");
//            result =  ((CadorgSaveCmd) o).execute(map);
//            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.cp.api.CadorgSaveCmd", "cp", "1.0");
            result =  cadorgSaveCmd.execute(map);
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            result = new ValueHolder();
            result.put("code", -1);
            result.put("message", e.getMessage());
        }

        return result.toJSONObject();

    }

    @ApiOperation(value = "用户的新增,修改用户的所属组织")
    @RequestMapping(path = "/api/cadusercmdimpl/save", method = RequestMethod.POST)
    public JSONObject cadusercmdimpl(HttpServletRequest request,
                                      @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", JSON.parseObject(param));
//            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.cp.api.CadUserSaveCmd", "cp", "1.0");
            result =  cadUserSaveCmd.execute(map);
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            result = new ValueHolder();
            result.put("code", -1);
            result.put("message", e.getMessage());
        }

        return result.toJSONObject();

    }

    @ApiOperation(value = "用户的新增,修改用户的所属组织")
    @RequestMapping(path = "/api/caduserpwdcmdimpl/modify", method = RequestMethod.POST)
    public JSONObject caduserpwdcmdimpl(HttpServletRequest request,
                                     @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.cp.api.CadUserPwdmodifyCmd", "cp", "1.0");
            result =  cadUserPwdmodifyCmd.execute(map);
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            result = new ValueHolder();
            result.put("code", -1);
            result.put("message", e.getMessage());
        }

        return result.toJSONObject();

    }

}

